<template>
  <div class="container">
    <div class="form-wrapper reset-request">
      <form @submit.prevent="requestReset">
        <div class="form-header">
          <h2>找回密码</h2>
          <p class="subtitle">请输入您的用户ID，我们将向您发送重置码</p>
        </div>

        <div class="input-group">
          <input
            type="text"
            required
            v-model="userId"
            :class="{ 'has-value': userId }"
          >
          <label>用户ID</label>
          <span class="input-line"></span>
        </div>

        <button
          type="submit"
          class="submit-btn"
          :disabled="isLoading"
        >
          <span v-if="!isLoading">发送重置码</span>
          <span v-else class="loading-spinner"></span>
        </button>

        <div class="form-footer">
          <router-link to="/login" class="back-link">
            <span class="arrow">←</span> 返回登录
          </router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import {ElMessage} from 'element-plus';
import {ref} from 'vue';
import {useRouter} from 'vue-router';
import axios from 'axios';

export default {
  setup() {
    const router = useRouter();
    const userId = ref('');
    const isLoading = ref(false);

    const requestReset = async () => {
      if (!userId.value) {
        ElMessage({
          message: '请输入用户ID',
          type: 'warning',
          duration: 2000,
          center: true,
        });
        return;
      }

      isLoading.value = true;

      try {
        await axios.post('/backend/api/account/password/reset_request/', {
          id: userId.value
        });

        ElMessage({
          message: '重置码已发送到您的邮箱',
          type: 'success',
          duration: 2000,
          center: true,
        });

        setTimeout(() => {
          router.push('/reset-password');
        }, 1500);

      } catch (error) {
        let errorMessage = '发送重置码失败，请稍后重试';

        if (error.response && error.response.status === 404) {
          errorMessage = '未找到该用户，请检查用户ID';
        }

        ElMessage({
          message: errorMessage,
          type: 'error',
          duration: 3000,
          center: true,
        });
      } finally {
        isLoading.value = false;
      }
    };

    return {
      userId,
      isLoading,
      requestReset
    };
  }
};
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.form-wrapper {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(10px);
  padding: 40px;
  border-radius: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

h2 {
  color: #fff;
  font-size: 32px;
  font-weight: 600;
  margin-bottom: 12px;
}

.subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  line-height: 1.5;
}

.input-group {
  position: relative;
  margin-bottom: 35px;
}

.input-group input {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  color: #fff;
  background: transparent;
  border: none;
  border-bottom: 2px solid rgba(255, 255, 255, 0.3);
  outline: none;
  transition: 0.3s;
}

.input-group label {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  font-size: 16px;
  color: rgba(255, 255, 255, 0.7);
  pointer-events: none;
  transition: 0.3s;
}

.input-group input:focus ~ label,
.input-group input.has-value ~ label {
  top: -20px;
  font-size: 12px;
  color: #fff;
}

.input-group .input-line {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background: #fff;
  transition: 0.3s;
}

.input-group input:focus ~ .input-line {
  width: 100%;
}

.submit-btn {
  width: 100%;
  padding: 16px;
  background: rgba(255, 255, 255, 0.2);
  border: none;
  border-radius: 12px;
  color: #fff;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: 0.3s;
  position: relative;
  overflow: hidden;
}

.submit-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.submit-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.loading-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.form-footer {
  margin-top: 24px;
  text-align: center;
}

.back-link {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 14px;
  transition: 0.3s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.back-link:hover {
  color: #fff;
}

.arrow {
  font-size: 18px;
}

/* 响应式适配 */
@media (max-width: 480px) {
  .form-wrapper {
    padding: 30px 20px;
  }

  h2 {
    font-size: 28px;
  }
}
</style>